package com.magnificent.atlantis.server.database.message;

import com.magnificent.atlantis.common.entity.messages.ServerMessage;
import com.magnificent.commons.db.datasource.ObjectEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * User: Babanin
 * Date: 06.05.2009
 * Time: 12:39:47
 */
public abstract class MessageHandler {
    private static final Map<Class, MessageHandler> handlers = new HashMap<Class, MessageHandler>();

    public static ObjectEntity messageReceived(ServerMessage message) {
        MessageHandler messageHandler = handlers.get(message.getClass());

        if (messageHandler != null) {
            return messageHandler.handleMessage(message);
        }

        return null;
    }

    public abstract ObjectEntity handleMessage(ServerMessage message);

    public static void register(Class messageClass, MessageHandler handler) {
        handlers.put(messageClass, handler);
    }
}
